<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        .box {
            background-color: antiquewhite;
            height: 100px;
            /* display: inline-block; */
            width: fit-content;
            /* 火狐兼容需要前缀 */
            width: -moz-fit-content;
        }

        .box1 {
            background-color: aquamarine;
            height: 100px;
        }
    </style>
</head>

<body>

    <div class="box">
        这是文字
        <p>文字</p>
    </div>

    <div class="box1">
        这是文字
        <p>文字</p>
    </div>
</body>

</html>